package com.baijiayun.liveuibase.widgets.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baijiayun.livecore.utils.CommonUtils;
import com.baijiayun.livecore.utils.LPLogger;
import com.baijiayun.liveuibase.R;
import com.baijiayun.liveuibase.utils.DisplayUtils;
import com.baijiayun.liveuibase.utils.ThemeDataUtil;
import com.baijiayun.liveuibase.utils.drawable.DrawableBuilder;

/**
 * Created by Shubo on 2018/9/21.
 */
public class CommonDialog extends BaseDialog {
    public enum ChoiceMode {
        None(0),
        Single_Negative(3),
        Single_Blue(1),
        Single_Red(2),
        Double_Blue(-1),
        Double_Red(-2);

        int mode;

        ChoiceMode(int mode) {
            this.mode = mode;
        }
    }

    /**
     * negative 键 风格
     */
    public enum NegativeType {
        Gray,
        Red,
    }

    public CommonDialog(@NonNull Context context, ChoiceMode choiceMode) {
        super(context, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
        this.context = context;
        this.choiceMode = choiceMode;
    }

    private Context context;
    private ChoiceMode choiceMode = ChoiceMode.None;
    private NegativeType negativeType = NegativeType.Gray;
    private String mainDisplayText;
    private String subtitleDisplayText;
    private int subtitleTextColor = -1;
    private String positiveText;
    private String negativeText;
    private int iconDrawableId;
    boolean showStudyReport;
    private DialogInterface.OnClickListener onPositiveClickListener;
    private DialogInterface.OnClickListener onNegativeClickListener;
    private DialogInterface.OnClickListener onStudyReportClickListener;

    /**
     * 主显示文字
     */
    public CommonDialog setMainDisplayText(String mainDisplayText) {
        this.mainDisplayText = mainDisplayText;
        return this;
    }

    /**
     * 副标题文字
     * @param subtitleDisplayText
     * @return
     */
    public CommonDialog setSubtitleDisplayText(String subtitleDisplayText) {
        this.subtitleDisplayText = subtitleDisplayText;
        return this;
    }

    public CommonDialog setSubtitleTextColor(int textColor) {
        this.subtitleTextColor = textColor;
        return this;
    }


    /**
     * icon drawable 资源id
     */
    public CommonDialog setIconDrawableId(int iconDrawableId) {
        this.iconDrawableId = iconDrawableId;
        return this;
    }

    /**
     * negative 按键 文本 及 点击事件
     */
    public CommonDialog setNegative(String negativeText, DialogInterface.OnClickListener onNegativeClickListener) {
        this.negativeText = negativeText;
        this.onNegativeClickListener = onNegativeClickListener;
        return this;
    }

    /**
     * negative 按键 风格
     */
    public CommonDialog setNegativeType(NegativeType negativeType) {
        this.negativeType = negativeType;
        return this;
    }

    /**
     * positive 按键 文本 及 点击事件
     */
    public CommonDialog setPositive(String positiveText, DialogInterface.OnClickListener onPositiveClickListener) {
        this.positiveText = positiveText;
        this.onPositiveClickListener = onPositiveClickListener;
        return this;
    }

    /**
     * 显示模式 单选，双选，无选择
     */
    public CommonDialog setDisplayMode(ChoiceMode choiceMode) {
        this.choiceMode = choiceMode;
        return this;
    }

    /**
     * 设置点击返回键关闭自身
     *
     * @param cancel 默认为true，传false则不收起
     */
    public CommonDialog setCancelOnPressBack(boolean cancel) {
        super.setCancelable(cancel);
        return this;
    }

    public CommonDialog setStudyReportBtn(boolean showStudyReport,DialogInterface.OnClickListener onStudyReportClickListener){
        this.showStudyReport = showStudyReport;
        this.onStudyReportClickListener = onStudyReportClickListener;
        return this;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // region 视图加载，控件初始化
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout container = new LinearLayout(context);
        container.setLayoutParams(new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        container.setGravity(Gravity.CENTER);
        View layout = inflater.inflate(R.layout.uibase_base_dialog_layout, container);

        ImageView ivIcon = layout.findViewById(R.id.bjysc_base_dialog_icon);
        TextView tvMain = layout.findViewById(R.id.bjysc_base_dialog_main_display);
        TextView subtitleTv = layout.findViewById(R.id.bjysc_base_dialog_subtitle);
        TextView tvNegative = layout.findViewById(R.id.bjysc_base_dialog_negative);
        TextView tvPositiveBlue = layout.findViewById(R.id.bjysc_base_dialog_positive_blue);
        TextView tvPositiveRed = layout.findViewById(R.id.bjysc_base_dialog_positive_red);
        TextView studyReportTv = layout.findViewById(R.id.bjysc_base_dialog_show_study_report);
        final Drawable drawable = new DrawableBuilder()
                .solidColor(ThemeDataUtil.getColorFromThemeConfigByAttrId(context, R.attr.base_theme_window_bg_color))
                .cornerRadius(context.getResources().getDimensionPixelSize(R.dimen.bjy_base_common_bg_radius))
                .strokeWidth(DisplayUtils.dip2px(context, 1))
                .strokeColor(ContextCompat.getColor(context, R.color.base_live_bg_stroke_10))
                .build();
        layout.findViewById(R.id.bjysc_common_dialog_root_container).setBackground(drawable);
        // endregion

        //region icon初始化
        Drawable iconDrawable;
        try {
            iconDrawable = ContextCompat.getDrawable(context, iconDrawableId);
        } catch (Exception e) {
            iconDrawable = null;
            LPLogger.e("CommonDialog", "iconDrawableId:(" + iconDrawableId + ") not found!!");
        }
        if (iconDrawable != null) {
            ivIcon.setImageDrawable(iconDrawable);
        } else {
            ivIcon.setVisibility(View.GONE);
        }
        // endregion

        tvMain.setText(mainDisplayText);
        if(TextUtils.isEmpty(subtitleDisplayText)) {
            subtitleTv.setVisibility(View.GONE);
        } else {
            subtitleTv.setText(subtitleDisplayText);
            subtitleTv.setVisibility(View.VISIBLE);
        }
        if(subtitleTextColor != -1) {
            subtitleTv.setTextColor(subtitleTextColor);
        }

        // region 按键初始化及监听
        if (TextUtils.isEmpty(negativeText) || choiceMode.mode >= 0) {
            tvNegative.setVisibility(View.GONE);
        } else {
            tvNegative.setText(negativeText);
            tvNegative.setTextColor(negativeType == NegativeType.Gray
                    ? CommonUtils.getColorByAttributeId(context, R.attr.base_theme_dialog_negative_text_color)
                    : getContext().getResources().getColor(R.color.base_live_warning_color));
            tvNegative.setOnClickListener(v -> {
                if (onNegativeClickListener != null) {
                    onNegativeClickListener.onClick(this, Dialog.BUTTON_NEGATIVE);
                }
                this.dismiss();
            });
        }
        if (TextUtils.isEmpty(positiveText)) {
            tvPositiveBlue.setVisibility(View.GONE);
            tvPositiveRed.setVisibility(View.GONE);
        } else {
            TextView positive = null;
            if (Math.abs(choiceMode.mode) == ChoiceMode.Single_Blue.mode) {
                positive = tvPositiveBlue;
                tvPositiveRed.setVisibility(View.GONE);
            } else if (Math.abs(choiceMode.mode) == ChoiceMode.Single_Red.mode) {
                positive = tvPositiveRed;
                tvPositiveBlue.setVisibility(View.GONE);
            }
            if (positive != null) {
                positive.setVisibility(View.VISIBLE);
                positive.setText(positiveText);
                positive.setOnClickListener(v -> {
                    if (onPositiveClickListener != null) {
                        onPositiveClickListener.onClick(this, Dialog.BUTTON_POSITIVE);
                    }
                    this.dismiss();
                });
            }
        }
        if(showStudyReport){
            studyReportTv.setVisibility(View.VISIBLE);
            studyReportTv.setOnClickListener(v->{
                if(onStudyReportClickListener!=null){
                    onStudyReportClickListener.onClick(this,Dialog.BUTTON_NEUTRAL);
                }
                this.dismiss();
            });
        }
        // endregion

        setContentView(layout);
    }
}
